package com.obelisco.vista.componentes;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.TooManyListenersException;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;

import tucana.echo2.app.FileUploadSelector;
import tucana.echo2.app.fileuploader.ProgressBar;
import tucana.echo2.app.fileuploader.UploadCallback;

import com.obelisco.vista.estilos.Styles;
import com.obelisco.vista.imagen.ByteArrayImageReference;

//import net.sf.ehcache.util.PropertyUtil;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.AwtImageReference;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.ResourceImageReference;
import nextapp.echo2.app.StreamImageReference;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.filetransfer.UploadEvent;
import nextapp.echo2.app.filetransfer.UploadListener;
import nextapp.echo2.app.filetransfer.UploadSelect;
import nextapp.echo2.app.layout.ColumnLayoutData;
import echopointng.ContainerEx;
import echopointng.ExtentEx;
import echopointng.ImageMap;
import echopointng.image.ImageKit;

public class PaneImagen2 extends ContainerEx {

	private ImageMap imgMapFoto;
	private ImageReference imgRefFoto;
	private Button btnCargar;
	private int ancho =150;
	private int alto = 250;
	
	private byte[] fileData;
	private String contentType = "";
	private long fileSize = 0;
	private String fileName ="";

	private Object objectBinding = null;
	private String propertyBinding = "";
	
	private boolean modificable = true;

	public PaneImagen2() {
		super();
		// Add design-time configured components.
		initComponents();
	}
	
	private void accionCargar(ActionEvent e) {
		//TODO Implement.
		DialogoSubirArchivo uploadDialog = new DialogoSubirArchivo("Cargar Archivo");
		uploadDialog.mostrarDialogo(450, 400);
	}
	
	private void initComponents() {
		this.setWidth(new Extent(ancho, Extent.PX));
		this.setHeight(new Extent(alto, Extent.PX));
		this.setInsets(new Insets(new Extent(10, Extent.PX)));
		Column column1 = new Column();
		add(column1);
		
		imgMapFoto = new ImageMap();
		imgRefFoto = Styles.ICON_NOFOTO;
		imgMapFoto.setImage(imgRefFoto);
		column1.add(imgMapFoto);
		
		btnCargar = new Button();
		btnCargar.setText("Cargar");
		btnCargar.setWidth(new ExtentEx("50%"));
		btnCargar.setStyleName("Obelisco.Boton");
		ColumnLayoutData btnCargarLayoutData = new ColumnLayoutData();
		btnCargarLayoutData.setAlignment(new Alignment(Alignment.RIGHT,
				Alignment.TOP));
		btnCargar.setLayoutData(btnCargarLayoutData);
		btnCargar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				accionCargar(e);
			}
		});
		column1.add(btnCargar);


        //Selector s = new Selector();
        //s.getFileuploadSelector().setButtonDisplayMode(FileUploadSelector.BUTTON_DISPLAY_RIGHT);
		//column1.add(s);
	}

	public int getAlto() {
		return alto;
	}

	public void setAlto(int alto) {
		this.alto = alto;
		this.setHeight(new Extent(alto, Extent.PX));
		this.imgMapFoto.setHeight(new Extent(alto, Extent.PX));
	}

	public int getAncho() {
		return ancho;
	}

	public void setAncho(int ancho) {
		this.ancho = ancho;
		this.setWidth(new Extent(ancho, Extent.PX));
		this.imgMapFoto.setWidth(new Extent(ancho, Extent.PX));
	}

	public ImageReference getImgRefFoto() {
		return imgRefFoto;
	}

	public void setImgRefFoto(ImageReference imgRefFoto) {
		this.imgRefFoto = imgRefFoto;
		imgMapFoto.setImage(this.imgRefFoto);
	}
	
	public byte[] getFileData() {
		return fileData;
	}

	public void setFileData(byte[] data, String contentType) {

        this.fileData = data;
        this.contentType = contentType;
        if (objectBinding != null && (propertyBinding != null && !propertyBinding.equals(""))) {
        	
        	
        	try {
				PropertyUtils.setProperty(objectBinding, propertyBinding, data);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	
        }
        
        if (data.length > 0) {
            setImgRefFoto(new ByteArrayImageReference(data,contentType));
        } else {
        	setImgRefFoto(Styles.ICON_NOFOTO);	
        }
		
	}
	
	public void setFileData(byte[] data) {
		
		setFileData(data,"image/jpg");

	}
	

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long imageSize) {
		this.fileSize = imageSize;
	}
	
	public void setBinding(Object objeto,String atributo) {
		
		 try {
			 this.objectBinding = objeto;
			 this.propertyBinding = atributo;
	
			 setFileData((byte[])PropertyUtils.getProperty(objeto, atributo));
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	class Selector extends Column {
	    
	    private FileUploadSelector fileuploadSelector;
	    
	    public Selector() {
	        setInsets(new Insets(6));

	        UploadCallback callback = new UploadCallback() {

				public void uploadFailed(tucana.echo2.app.fileuploader.UploadEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				public void uploadSucceeded(tucana.echo2.app.fileuploader.UploadEvent event) {
					// TODO Auto-generated method stub
	                try {
	                    ByteArrayOutputStream out = new ByteArrayOutputStream();
	                    IOUtils.copy(event.getInputStream(), out);
	                    event.getInputStream().close();
	                    byte[] fileBytes = out.toByteArray();
	                    final String ct = event.getContentType();
	                    setFileData(fileBytes, ct);
	                    setFileName(event.getFileName());
	                    setFileSize(event.getFileSize());
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
				}
	            
	        };
	        
	        fileuploadSelector = new FileUploadSelector();
	        //fileuploadSelector.setWidthSize(40);
	        //fileuploadSelector.setWidthExtent(new Extent(100, Extent.PERCENT));
	        //fileuploadSelector.setWidthMode(FileUploadSelector.WIDTH_MODE_EXTENT);
	        fileuploadSelector.setUploadCallback(callback);
	        
	        
	        
	        fileuploadSelector.setButtonMode(FileUploadSelector.BUTTON_MODE_TEXT);
	        add(fileuploadSelector);
	        add(new ProgressBar(fileuploadSelector));
	        
	    }

		public FileUploadSelector getFileuploadSelector() {
			return fileuploadSelector;
		}

		public void setFileuploadSelector(FileUploadSelector fileuploadSelector) {
			this.fileuploadSelector = fileuploadSelector;
		}
	}


	public boolean isModificable() {
		return modificable;
	}

	public void setModificable(boolean modificable) {
		this.modificable = modificable;
		btnCargar.setVisible(modificable);
	}
	
	
}

